<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        function WebSocketTest() {
           if ("WebSocket" in window) {
              console.log("您的浏览器支持 WebSocket!");
              // 打开一个 web socket
            //   var ws = new WebSocket("ws://localhost:8080/handler");
              var ws = new WebSocket("ws://localhost:18081/bcenter/handler");
               
              ws.onopen = function() {
                 // Web Socket 已连接上，使用 send() 方法发送数据
                 var msg = {type:"1",body:"zhansan"};
                 ws.send(JSON.stringify(msg));
                 console.log("数据发送中...");
              };
               
              ws.onmessage = function (evt) { 
                 var received_msg = evt.data;
                 console.log("接收数据："+received_msg);
              };
               
              ws.onclose = function() { 
                 // 关闭 websocket
                 console.log("连接已关闭..."); 
              };
           } else {
              // 浏览器不支持 WebSocket
              console.log("您的浏览器不支持 WebSocket!");
           }
        }
        WebSocketTest();
     </script>
</body>
</html>